package com.yj.discovery.service;

import com.yj.common.enums.InventoryType;
import com.yj.common.enums.TransportType;
import com.yj.discovery.entity.DiscoveryInfo;
import com.yj.discovery.entity.DiscoveryInventoryItem;
import com.yj.discovery.entity.DiscoveryPCInfo;
import com.yj.discovery.mapper.DiscoveryMapper;
import com.yj.inventory.entity.Inventory;
import com.yj.transport.entity.TransportInfo;
import com.yj.transport.entity.TransportJournal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/13
 * Author     : Unbelievable9
 **/
@Service
public class DiscoveryService {

    @Autowired
    DiscoveryMapper discoveryMapper;

    public DiscoveryInfo getInfo(Integer limit) {
        DiscoveryInfo discoveryInfo = new DiscoveryInfo();
        discoveryInfo.setDiscoveryItemList(discoveryMapper.getItemByLimit(limit));
        discoveryInfo.setTrain_count(discoveryMapper.totalCountForTransportType(TransportType.TRAIN.getValue()));
        discoveryInfo.setShip_count(discoveryMapper.totalCountForTransportType(TransportType.SHIP.getValue()));
        discoveryInfo.setCar_count(discoveryMapper.totalCountForTransportType(TransportType.CAR.getValue()));

        BigDecimal totalCoalAmount = discoveryMapper.totalCoalAmountForTransportStatus(0);

        if (totalCoalAmount != null) {
            discoveryInfo.setCoal_amount(totalCoalAmount.intValue());
        }

        return discoveryInfo;
    }

    public DiscoveryPCInfo getPCInfo() {
        DiscoveryPCInfo discoveryPCInfo = new DiscoveryPCInfo();
        discoveryPCInfo.setTrain_count(discoveryMapper.totalCountForTransportType(TransportType.TRAIN.getValue()));
        discoveryPCInfo.setShip_count(discoveryMapper.totalCountForTransportType(TransportType.SHIP.getValue()));
        discoveryPCInfo.setCar_count(discoveryMapper.totalCountForTransportType(TransportType.CAR.getValue()));

        BigDecimal contractAmount = discoveryMapper.totalCoalAmountForContract();

        if (contractAmount == null) {
            contractAmount = BigDecimal.ZERO;
        }

        discoveryPCInfo.setApproving_contract_amount(contractAmount);

        BigDecimal transferAmount = discoveryMapper.totalCoalAmountForTransportStatus(0);

        if (transferAmount == null) {
            transferAmount = BigDecimal.ZERO;
        }
        discoveryPCInfo.setTransfer_contract_amount(transferAmount);

        BigDecimal checkingAmount = discoveryMapper.totalCoalAmountForTransportStatus(1);

        if (checkingAmount == null) {
            checkingAmount = BigDecimal.ZERO;
        }

        discoveryPCInfo.setChecking_contract_amount(checkingAmount);

        List<DiscoveryInventoryItem> itemList = new ArrayList<>();

        for (int i = 1; i <= InventoryType.getSize(); i++) {
            DiscoveryInventoryItem item = new DiscoveryInventoryItem();
            item.setType(InventoryType.fromValue(i));
            item.setCount(discoveryMapper.countForInventoryByType(i));

            BigDecimal amount = discoveryMapper.totalInventoryAmountForType(i);

            if (amount == null) {
                amount = BigDecimal.ZERO;
            }

            item.setAmount(amount);

            itemList.add(item);
        }

        discoveryPCInfo.setInventory_list(itemList);

        return discoveryPCInfo;
    }

    public List<Map<String, Object>> getTransportInfo(Integer limit) {
        List<Map<String, Object>> infoList = new ArrayList<>();

        List<TransportInfo> transportInfoList = discoveryMapper.getTransportInfoByLimit(limit);

        List<Integer> idList = new ArrayList<>();

        for (TransportInfo transportInfo : transportInfoList) {
            idList.add(transportInfo.getId());
        }

        List<TransportJournal> transportJournalList = discoveryMapper.getTransportJournal(idList);

        for (TransportInfo transportInfo : transportInfoList) {
            Map<String, Object> infoMap = new HashMap<>();
            infoMap.put("transport_info", transportInfo);

            List<TransportJournal> targetJournalList = new ArrayList<>();

            for (TransportJournal transportJournal : transportJournalList) {
                if (transportJournal.getTransport_info_id().equals(transportInfo.getId())) {
                    targetJournalList.add(transportJournal);
                }
            }

            infoMap.put("transport_journal", targetJournalList);

            infoList.add(infoMap);
        }

        return infoList;
    }

    public List<Inventory> getPCInventoryInfo(Integer limit) {
        return discoveryMapper.getAllInventory(limit);
    }
}
